<?php

namespace App\Admin\Views\SwtProcurement;

use App\Models\SwtGoodQuantity;
use App\Models\SwtMachine;
use App\Models\SwtReplenishment;
use App\Models\SwtGood;
use Dcat\Admin\Support\LazyRenderable;
use Throwable;

class SwtReplenishmentView extends LazyRenderable
{

    /**
     * Get the evaluated contents of the object.
     *
     * @return array|string
     * @throws Throwable
     */
    public function render()
    {
        $order_info = SwtReplenishment::with(['goods'])->where('id', $this->payload['id']) -> first();
        $order_info-> machine_name = SwtMachine::where('id',$order_info->replenishment_machine_id)->first()->machine_name ?? '';
        switch ($order_info -> replenishment_status) {
            case 0:
                $order_info -> replenishment_status_word = '未出库';
                $order_info -> color = "background:#ea5455";
                break;
            case 1:
                $order_info -> replenishment_status_word = '出库';
                $order_info -> color = "background:#3085d6";
                break;
            case 2:
                $order_info -> replenishment_status_word = '补货完成';
                $order_info -> color = "background:#21b978";
                break;
            default:
                $order_info -> replenishment_status_word = '无状态显示';
                $order_info -> color = "background:#dda451";
        }
        $order_info->cost_prices = bcdiv($order_info->cost_prices,1000,2);
        $order_info->leave_image = "https://static.device.shuwentong.com/".$order_info->leave_image;
        $order_info->back_image = "https://static.device.shuwentong.com/".$order_info->back_image;
        foreach($order_info->goods as &$goods){
            $swt_goods = SwtGood::with('good_quantity')->where('id',$goods->good_id)->first();
            $good_quantity = $swt_goods->good_quantity;
            if($swt_goods != null){
                $goods->goods_name = $swt_goods->goods_name;
                $goods->cost_price = bcdiv($swt_goods->cost_price,1000,2);
                $goods->goods_quantity = $good_quantity['normal_quantity'] + $good_quantity['loss_quantity'];
            }else{
                $goods->goods_name = '';
                $goods->cost_price = 0;
                $goods->goods_quantity = '';
            }
            $goods->goods_price /= 100;
            $goods->advance_price = bcdiv($goods->advance_price,100,2);
        }

        return admin_view('admin.replenishment.detail', ['order_info' => $order_info]);
    }

}

